package com.d.lib.ui.view.recordtrigger;

import com.d.lib.ui.view.ResourceTable;
import java.io.InputStream;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

;

/**
 * 指示器
 * Created by D on 2017/11/1.
 */
public class RecordLightView extends DirectionalLayout {
  private final String toastUp = "手指上滑，取消发送";
  private final String toastCancel = "松开手指，取消发送";
  private Image ivFlag;
  private Text tvToast;

  public RecordLightView(Context context) {
    super(context);
    init(context);
  }

  public RecordLightView(Context context, AttrSet attrs) {
    super(context, attrs);
    init(context);
  }

  public RecordLightView(Context context, AttrSet attrs, String defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
  }

  private void init(Context context) {
    Component root =
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_lib_ui_view_rtv_view,
            this, true);
    ShapeElement shapeElement = new ShapeElement();
    shapeElement.setShape(ShapeElement.RECTANGLE);
    shapeElement.setCornerRadius(18);
    shapeElement.setRgbColor(RgbColor.fromRgbaInt(0x6A6A6A99));
    root.setBackground(shapeElement);
    ivFlag = (Image) root.findComponentById(ResourceTable.Id_iv_flag);
    tvToast = (Text) root.findComponentById(ResourceTable.Id_tv_toast);
  }

  public void setState(int state) {
    switch (state) {
      case RecordTriggerView.STATE_VALID:
        setVisibility(VISIBLE);
        PixelMap p;
        InputStream inputStream;
        ImageSource imageSource;
        try {
          inputStream =
              getResourceManager().getResource(ResourceTable.Media_lib_ui_view_rtv_ic_voice);
          imageSource = ImageSource.create(inputStream, null);
          p = imageSource.createPixelmap(null);
          ivFlag.setPixelMap(p);
        } catch (Exception e) {
          e.printStackTrace();
        }
        tvToast.setBackground(new ShapeElement());
        tvToast.setText(toastUp);
        break;
      case RecordTriggerView.STATE_INVALID:
        setVisibility(VISIBLE);
        PixelMap p1;
        InputStream inputStream1;
        ImageSource imageSource1;
        try {
          inputStream =
              getResourceManager().getResource(ResourceTable.Media_lib_ui_view_rtv_ic_back);
          imageSource1 = ImageSource.create(inputStream, null);
          p1 = imageSource1.createPixelmap(null);
          ivFlag.setPixelMap(p1);
        } catch (Exception e) {
          e.printStackTrace();
        }

        ShapeElement vectorElement2 = new ShapeElement(getContext(),
            ResourceTable.Layout_lib_ui_view_rtv_corner_bg);
        tvToast.setBackground(vectorElement2);
        tvToast.setText(toastCancel);
        break;
      case RecordTriggerView.STATE_CANCLE:
        setVisibility(HIDE);
        break;
      case RecordTriggerView.STATE_SUBMIT:
        setVisibility(HIDE);
        break;
    }
  }
}
